﻿using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Windows.Input;
using AdTech.Common.WPF;

namespace AdTech.Snap
{
    #pragma warning disable 1591
    public abstract class ViewModelBase : Validator, INotifyPropertyChanged
    {
        // Fields
        private bool? _DialogResult;
        private DelegateCommand _CloseCommand;

        // Properties
        internal Action RequestClose { get; set; }
        public bool? DialogResult
        {
            get { return _DialogResult; }
            set { if (value == _DialogResult) return; _DialogResult = value; NotifyPropertyChanged(() => this.DialogResult); }
        }

        #region Commands
        public ICommand CloseCommand
        {
            get
            {
                if (_CloseCommand == null)
                    _CloseCommand = new DelegateCommand(OnExecuteClose);
                return _CloseCommand;
            }
        }
        #endregion

        // Constructor
        public ViewModelBase()
        {

        }

        // Methods
        #region Commands
        protected virtual void OnExecuteClose()
        {
            if (this.RequestClose != null)
                this.RequestClose();
        }
        #endregion

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        protected void NotifyPropertyChanged<T>(Expression<Func<T>> propertyExpresssion)
        {
            string member = (propertyExpresssion.Body as MemberExpression).Member.Name;
            this.NotifyPropertyChanged(member);
        }
        #endregion
    }
    #pragma warning restore 1591
}
